package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.MainActivity;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.RegisterBean;
import com.lc.luzhengtang.bean.VerificationCodeBean;
import com.lc.luzhengtang.conn.ConnRegister;
import com.lc.luzhengtang.conn.ConnVerificationCode;
import com.lc.luzhengtang.util.LovelyToast;
import com.lc.luzhengtang.util.Validator;
import com.lc.luzhengtang.view.ClearEditText;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2017/5/3.
 */

public class RegisterActivity extends BaseActivity {

    int a = 2;
    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.img_phone)
    ImageView imgPhone;
    @BindView(R.id.edit_phone)
    ClearEditText editPhone;
    @BindView(R.id.img_sendcode)
    ImageView imgSendcode;
    @BindView(R.id.edit_code)
    ClearEditText editCode;
    @BindView(R.id.layout_send_ben)
    LinearLayout layoutSendBen;
    @BindView(R.id.text_sendbtn)
    TextView textSendbtn;
    @BindView(R.id.img_pwd)
    ImageView imgPwd;
    @BindView(R.id.edit_newpwd)
    ClearEditText editNewpwd;
    @BindView(R.id.img_submit)
    ImageView imgSubmit;
    @BindView(R.id.edit_repwd)
    ClearEditText editRepwd;
    @BindView(R.id.img_tuijian)
    ImageView imgTuijian;
    @BindView(R.id.edit_tuijiancode)
    ClearEditText editTuijiancode;
    @BindView(R.id.checkbox_selectxieyi)
    CheckBox checkboxSelectxieyi;
    @BindView(R.id.register_aggrement)
    TextView registerAggrement;
    @BindView(R.id.btn_register)
    Button btnRegister;
    boolean is_matcher;

    String myCode;


    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                textSendbtn.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                textSendbtn.setText("重新获取");
                textSendbtn.setEnabled(true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);

        textTitle.setText("注册");
        checkboxSelectxieyi.setChecked(true);

        checkboxSelectxieyi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    a = 2;
                } else {
                    a = 1;
                }
            }
        });

    }

    @OnClick({R.id.img_news, R.id.text_title, R.id.img_phone, R.id.edit_phone, R.id.img_sendcode, R.id.edit_code, R.id.layout_send_ben, R.id.text_sendbtn, R.id.img_pwd, R.id.edit_newpwd, R.id.img_submit, R.id.edit_repwd, R.id.img_tuijian, R.id.edit_tuijiancode, R.id.checkbox_selectxieyi, R.id.register_aggrement, R.id.btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_title:
                break;
            case R.id.img_phone:
                break;
            case R.id.edit_phone:
                break;
            case R.id.img_sendcode:
                break;
            case R.id.edit_code:
                break;
            case R.id.layout_send_ben:
                break;
            case R.id.text_sendbtn:
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    textSendbtn.setEnabled(false);
                    getVerificationCode();
                }

                break;
            case R.id.img_pwd:
                break;
            case R.id.edit_newpwd:
                break;
            case R.id.img_submit:
                break;
            case R.id.edit_repwd:
                break;
            case R.id.img_tuijian:
                break;
            case R.id.edit_tuijiancode:
                break;
            case R.id.checkbox_selectxieyi:
                break;
            case R.id.register_aggrement:
                startActivity(new Intent(RegisterActivity.this, NoticeActivity2.class));
                break;
            case R.id.btn_register:
                is_matcher = editNewpwd.getText().toString() != null && editNewpwd.getText().toString().matches("[A-Za-z0-9]+");
                String regEx = "[A-Z,a-z,0-9,-]*";
                boolean result = Pattern.compile(regEx).matcher(editNewpwd.getText().toString()).find();
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else if (TextUtils.isEmpty(editCode.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "验证码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!editCode.getText().toString().equals(myCode)) {

                    LovelyToast.makeText(RegisterActivity.this, "验证码不正确", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editNewpwd.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!Validator.isPassword(editNewpwd.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "请输入6~12位数字或英文密码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editRepwd.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!Validator.isPassword(editRepwd.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "请输入6~12位数字或英文密码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!editNewpwd.getText().toString().equals(editRepwd.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "两次输入的密码不一致", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } /*else if (result) {
                    LovelyToast.makeText(RegisterActivity.this, "密码应为数字与字母的组合", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }*/ else if (a == 1) {
                    LovelyToast.makeText(RegisterActivity.this, "请勾选注册协议", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    getRegister();
                }
                break;
        }
    }

    public static boolean isLetterDigitOrChinese(String str) {
        String regex = "^[a-z0-9]+$";
        return str.matches(regex);
    }
/*
    @OnClick({R.id.img_news, R.id.edit_phone, R.id.edit_code, R.id.text_sendbtn, R.id.edit_newpwd, R.id.edit_repwd, R.id.edit_tuijiancode, btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.edit_phone:
                break;
            case R.id.edit_code:
               *//* if (editPhone.getText().toString().equals("")) {

                    //UtilToast.show(this, "请输入手机号");
                    LovelyToast.makeText(RegisterActivity.this, "请输入手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }


                if (!UtilMatches.checkMobile(editPhone.getText().toString())) {

                    LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {

                    editCode.setEnabled(false);
                    getVerificationCode();
                }
*//*

                break;
            case R.id.text_sendbtn:

                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPhone.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(RegisterActivity.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    textSendbtn.setEnabled(false);
                    getVerificationCode();
                }


                break;
            case R.id.edit_newpwd:
                break;
            case R.id.edit_repwd:
                break;
            case edit_tuijiancode:
                break;

            case btn_register:

                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPhone.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(RegisterActivity.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else if (TextUtils.isEmpty(editCode.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "验证码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editNewpwd.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editNewpwd.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editNewpwd.getText().toString().length() < 6) {
                    LovelyToast.makeText(RegisterActivity.this, "请输入6~12位的密码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editRepwd.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!editNewpwd.getText().toString().equals(editRepwd.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "两次输入的密码不一致", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {

                    getRegister();
                }
                break;



        }
    }


*/

    String code = "";

    public void getVerificationCode() {
        new ConnVerificationCode(editPhone.getText().toString().trim(), "reg", new AsyCallBack<VerificationCodeBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, VerificationCodeBean registerBean) throws Exception {

                LovelyToast.makeText(RegisterActivity.this, registerBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
//                btRegistered.setSelected(true);
                countTime.start();
                myCode = registerBean.getData().getCode() + "";
                super.onSuccess(toast, type, o, registerBean);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
                textSendbtn.setEnabled(false);
                textSendbtn.setText("获取验证码");
                countTime.start();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
//                countTime.start();
            }
        }


        ).execute(this);
    }

    public void getRegister() {
        Log.d(editPhone.getText().toString().trim(), "手机号");
        Log.d(editCode.getText().toString().trim(), "验证码");
        Log.d(editNewpwd.getText().toString().trim(), "密码");
//        edit_tuijiancode
        new ConnRegister(editPhone.getText().toString().trim(), editNewpwd.getText().toString().trim(), editTuijiancode.getText().toString(), editCode.getText().toString().trim(), new AsyCallBack<RegisterBean>() {

            @Override
            public void onSuccess(String toast, int type, Object o, RegisterBean registerBean) throws Exception {

                if (registerBean.getCode() == 200) {
                    LovelyToast.makeText(RegisterActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    BaseApplication.basePreferences.saveUid(registerBean.getData().getId() + "");
                    BaseApplication.basePreferences.saveMobile(editPhone.getText().toString().trim());

//                finish();
                    startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                } else {
                    LovelyToast.makeText(RegisterActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);


                LovelyToast.makeText(RegisterActivity.this, "注册失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);

            }
        }


        ).execute(this);

    }
}
